package com.lincoln.redis.server.controller;

import com.lincoln.bean.ApiCode;
import com.lincoln.bean.ApiCodeException;
import com.lincoln.redis.server.bean.Page;
import com.lincoln.redis.server.entity.User;
import com.lincoln.redis.server.entity.UserVo;
import com.lincoln.redis.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户接口
 *
 * @author admin
 */
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @GetMapping("/list")
    public ApiCode list(Integer pageNo, Integer pageSize, String username) {
        List<User> list;
        if (username != null && "".equals(username)) {
            list = userService.findAll();
        } else {
            list = userService.findAll(username == null ? "" : username);
        }
        List<UserVo> res = UserVo.formatList(list);
        return ApiCode.build(res);
    }

    /**
     * 获取当前token的用户
     *
     * @return
     */
    @GetMapping()
    @PreAuthorize("isAuthenticated()")
    public ApiCode currentUser() {
        User user = userService.findByName((String) SecurityContextHolder.getContext().getAuthentication().getPrincipal());
        return ApiCode.build(new UserVo(user));
    }

    @GetMapping("/{id}")
    public ApiCode simple(@PathVariable Long id) throws ApiCodeException {
        User user = userService.findById(id);
        if (user == null) {
            throw new ApiCodeException(11002);
        }
        return ApiCode.build(new UserVo(user));
    }

    @PutMapping()
    @PreAuthorize("hasAuthority('admin')")
    public ApiCode save(String username, String password) throws ApiCodeException {
        if (username == null || "".equals(username) || password == null || "".equals(password)) {
            throw new ApiCodeException(10001);
        }
        if (userService.findByName(username) != null) {
            throw new ApiCodeException(11001);
        }
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setEnabled(true);
        user.setAccountNonExpired(true);
        user.setAccountNonLocked(true);
        user.setCredentialsNonExpired(true);
        userService.save(user);
        return ApiCode.build(true);
    }

    @DeleteMapping()
    @PreAuthorize("hasAuthority('admin')")
    public ApiCode delete() throws ApiCodeException {
        for (User user : userService.findAll()) {
            if (!"admin".equals(user.getUsername())) {
                userService.delete(user);
            }
        }
        return ApiCode.build();
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('admin')")
    public ApiCode delete(@PathVariable Integer id) throws ApiCodeException {
        if (id == null) {
            throw new ApiCodeException(10001);
        }
        User user = userService.findById(id);
        if (user == null) {
            throw new ApiCodeException(11002);
        }
        return ApiCode.build(userService.delete(user));
    }
}
